package edu.uta.BillMeApp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import edu.BillME.datapars.HttpConnection;
import edu.uta.BillMeApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/* @auther Siddharth Patel & Susan Foster */
public class Register extends Activity {
     
	EditText username, password, email,firstname,lastname;
	Button register;
	String res;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        Log.d("register inside", "its working");
        firstname=(EditText)findViewById(R.id.fname);
        lastname=(EditText)findViewById(R.id.lname);
        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        email=(EditText)findViewById(R.id.email);
        register=(Button)findViewById(R.id.submitreg
        		);
        Log.d("Edit text", "its working");
        
        	register.setOnClickListener(new View.OnClickListener() {
        		//@Override
        		public void onClick(View v) {
        			// TODO Auto-generated method stub
        			ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        			postParameters.add(new BasicNameValuePair("firstname", firstname.getText().toString()));
        			postParameters.add(new BasicNameValuePair("lastname", lastname.getText().toString()));
        			postParameters.add(new BasicNameValuePair("username", username.getText().toString()));
        			postParameters.add(new BasicNameValuePair("password", password.getText().toString()));
        			postParameters.add(new BasicNameValuePair("email", email.getText().toString()));

        			String response = null;
        			try {
						response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/register.php", postParameters);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						Log.w("exception",e.toString());
					}
        			res=response.toString();
        			res=res.replace("\n", "");
        			if(res.contains("1")){
        				Toast.makeText(getApplicationContext(), "Registration successful" ,Toast.LENGTH_LONG).show();
        				}
        				else{
        					Toast.makeText(getApplicationContext(), "Registration failed" ,Toast.LENGTH_LONG).show();	
        				}
        		}

        
        	});
    }
        
}